package 力扣;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author yyq
 * @create 2022-04-11 14:29
 */
public class leetcode513 {
    public static void main(String[] args) {
        TreeNode root=new TreeNode(1);
        root.left=new TreeNode(2);
        root.right=new TreeNode(3);
        System.out.println(findBottomLeftValue(root));

    }
    public static int findBottomLeftValue(TreeNode root) {
        return levelTraversal(root);
    }

    private static int levelTraversal(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        int layerNodes = 1;
        queue.add(root);
        while (!queue.isEmpty()) {
            int temp = 0;
            int one = 1;
            TreeNode tempNode = null;
            while (layerNodes != 0) {

                TreeNode node = queue.poll();
                if (one == 1) {
                    tempNode = node;
                    one--;
                }
                if (node.left != null) {
                    queue.add(node.left);
                    temp++;
                }
                if (node.right != null) {
                    queue.add(node.right);
                    temp++;
                }
                layerNodes--;
            }
            if (temp == 0) return tempNode.val;
            layerNodes = temp;
        }
        return -1;
    }
}
